﻿using Dapper;
using System;
using BCEntity.TMS.EasyMoveSuiteConfigTemplate;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.TMS.EasyMoveSuiteConfigTemplate
{
	/// <summary>
	/// 便捷搬家套餐配置模板
	/// </summary>
	public class EasyMoveSuiteConfigTemplateData : IEasyMoveSuiteConfigTemplateData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM easymovesuiteconfigtemplate";
		private const string QUERY = @"SELECT A.EasyMoveSuiteConfigTemplateId,A.CreateAdminId,A.CreateAdminName,A.CreateTime,A.UpdateTime,A.EditAdminId,A.EditAdminName,A.SuiteName,
											A.VehicleBusinessMappingId,A.VehicleTypeId,A.SuiteForCrowdTitle,A.SuiteForCrowdRemark,A.SuiteServiceTitle,A.SuiteServiceRemark,A.Enabled,
											A.CarryServiceAmount,A.CarryServiceRemark,
											B.TransportBusiness,C.VehicleTypeName,C.VehicleTypeEnum,C.VehiclePicPath
										FROM easymovesuiteconfigtemplate A
										INNER JOIN VehicleBusinessMapping B ON A.VehicleBusinessMappingId=B.VehicleBusinessMappingId
										INNER JOIN VehicleType C ON B.VehicleTypeId=C.VehicleTypeId";
		public EasyMoveSuiteConfigTemplateData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查便捷搬家套餐配置模板是否存在
		public async Task<bool> Exists(int easyMoveSuiteConfigTemplateId)
		{
			string sql = string.Format(@"{0} WHERE EasyMoveSuiteConfigTemplateId=?EasyMoveSuiteConfigTemplateId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?EasyMoveSuiteConfigTemplateId", easyMoveSuiteConfigTemplateId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		public async Task<bool> Exists(int vehicleBusinessMappingId, string suiteName)
		{
			string sql = string.Format(@"{0} WHERE VehicleBusinessMappingId=?VehicleBusinessMappingId AND SuiteName=?SuiteName LIMIT 1", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleBusinessMappingId", vehicleBusinessMappingId, DbType.Int32);
			parameters.Add("?SuiteName", suiteName, DbType.AnsiString);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加便捷搬家套餐配置模板
		public async Task<EasyMoveSuiteConfigTemplateEntity> Add(EasyMoveSuiteConfigTemplateEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("easymovesuiteconfigtemplate", new string[] {  "SuiteName", "VehicleBusinessMappingId","CarryServiceAmount","CarryServiceRemark",
				"VehicleTypeId", "SuiteForCrowdTitle", "SuiteForCrowdRemark", "SuiteServiceTitle", "SuiteServiceRemark","CreateAdminId","CreateAdminName" });
			sql = string.Format("{0} {1} WHERE A.EasyMoveSuiteConfigTemplateId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?UpdateTime", entity.UpdateTime, DbType.DateTime);
			parameters.Add("?SuiteName", entity.SuiteName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleBusinessMappingId", entity.VehicleBusinessMappingId, DbType.Int32, size: 11);
			parameters.Add("?VehicleTypeId", entity.VehicleTypeId, DbType.Int32, size: 11);
			parameters.Add("?SuiteForCrowdTitle", entity.SuiteForCrowdTitle?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteForCrowdRemark", entity.SuiteForCrowdRemark?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteServiceTitle", entity.SuiteServiceTitle?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteServiceRemark", entity.SuiteServiceRemark?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?CarryServiceRemark", entity.CarryServiceRemark?.Trim(), DbType.String);
			parameters.Add("?CarryServiceAmount", entity.CarryServiceAmount, DbType.Decimal);
			parameters.Add("?Enabled", entity.Enabled, DbType.Boolean);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<EasyMoveSuiteConfigTemplateEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改便捷搬家套餐配置模板
		public async Task<EasyMoveSuiteConfigTemplateEntity> Update(EasyMoveSuiteConfigTemplateEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("easymovesuiteconfigtemplate", new string[] { "EditAdminId", "EditAdminName", "SuiteName", "VehicleBusinessMappingId", "VehicleTypeId",
				"SuiteForCrowdTitle", "SuiteForCrowdRemark", "SuiteServiceTitle", "SuiteServiceRemark","CarryServiceAmount","CarryServiceRemark", }, new string[] { "EasyMoveSuiteConfigTemplateId" });
			sql = string.Format("{0} {1} WHERE A.EasyMoveSuiteConfigTemplateId=?EasyMoveSuiteConfigTemplateId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?EasyMoveSuiteConfigTemplateId", entity.EasyMoveSuiteConfigTemplateId, DbType.Int32, size: 11);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?SuiteName", entity.SuiteName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleBusinessMappingId", entity.VehicleBusinessMappingId, DbType.Int32, size: 11);
			parameters.Add("?VehicleTypeId", entity.VehicleTypeId, DbType.Int32, size: 11);
			parameters.Add("?SuiteForCrowdTitle", entity.SuiteForCrowdTitle?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteForCrowdRemark", entity.SuiteForCrowdRemark?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteServiceTitle", entity.SuiteServiceTitle?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteServiceRemark", entity.SuiteServiceRemark?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?CarryServiceRemark", entity.CarryServiceRemark?.Trim(), DbType.String);
			parameters.Add("?CarryServiceAmount", entity.CarryServiceAmount, DbType.Decimal);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<EasyMoveSuiteConfigTemplateEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除便捷搬家套餐配置模板
		public async Task<bool> Delete(int easyMoveSuiteConfigTemplateId)
		{
			string sql = @"DELETE FROM easymovesuiteconfigtemplate WHERE EasyMoveSuiteConfigTemplateId=?EasyMoveSuiteConfigTemplateId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?EasyMoveSuiteConfigTemplateId", easyMoveSuiteConfigTemplateId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取便捷搬家套餐配置模板
		public async Task<EasyMoveSuiteConfigTemplateEntity> GetEntity(int easyMoveSuiteConfigTemplateId)
		{
			string sql = string.Format(@"{0} WHERE EasyMoveSuiteConfigTemplateId=?EasyMoveSuiteConfigTemplateId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?EasyMoveSuiteConfigTemplateId", easyMoveSuiteConfigTemplateId, DbType.Int64);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<EasyMoveSuiteConfigTemplateEntity>(sql, parameters);
		}
		public async Task<EasyMoveSuiteConfigTemplateEntity> GetEntity(int vehicleBusinessMappingId, string suiteName)
		{
			string sql = string.Format(@"{0} WHERE A.VehicleBusinessMappingId=?VehicleBusinessMappingId AND A.SuiteName=?SuiteName LIMIT 1", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleBusinessMappingId", vehicleBusinessMappingId, DbType.Int32);
			parameters.Add("?SuiteName", suiteName, DbType.AnsiString);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<EasyMoveSuiteConfigTemplateEntity>(sql, parameters);
		}
		#endregion

		#region 获取便捷搬家套餐配置模板列表
		public async Task<IEnumerable<EasyMoveSuiteConfigTemplateEntity>> GetList()
		{
			string sql = string.Format(@"{0} WHERE A.Enabled=1", QUERY);
			return await this.databaseContext.Master.QueryAsync<EasyMoveSuiteConfigTemplateEntity>(sql);
		}
		#endregion

		#region 获取便捷搬家套餐配置模板分页
		public async Task<(IEnumerable<EasyMoveSuiteConfigTemplateEntity>, int)> Query(string suiteName, bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();
			if (!string.IsNullOrWhiteSpace(suiteName))
			{
				wheres.Add("A.SuiteName LIKE CONCAT('%',CONCAT(?SuiteName,'%'))");
				parameters.Add("?SuiteName", suiteName, DbType.AnsiString);
			}
			if (enabled.HasValue)
			{
				wheres.Add("A.Enabled=?Enabled");
				parameters.Add("?Enabled", enabled.Value, DbType.Boolean);
			}
			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("A.CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("A.CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("A.CreateTime>=?Start AND A.CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM easymovesuiteconfigtemplate A 
							INNER JOIN VehicleBusinessMapping B ON A.VehicleBusinessMappingId=B.VehicleBusinessMappingId
							INNER JOIN VehicleType C ON B.VehicleTypeId=C.VehicleTypeId {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "A.CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.QueryMultipleAsync<EasyMoveSuiteConfigTemplateEntity, int>(sql, parameters);
		}
		#endregion

		#region 启用便捷搬家套餐配置模板
		public async Task<bool> Enabled(int easyMoveSuiteConfigTemplateId, long adminId, string fullname)
		{
			string sql = @"UPDATE easymovesuiteconfigtemplate SET Enabled=1,EditAdminId=?EditAdminId,EditAdminName=?EditAdminName WHERE EasyMoveSuiteConfigTemplateId=?EasyMoveSuiteConfigTemplateId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?EasyMoveSuiteConfigTemplateId", easyMoveSuiteConfigTemplateId, DbType.Int32);
			parameters.Add("?EditAdminId", adminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", fullname, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 禁用便捷搬家套餐配置模板
		public async Task<bool> Disabled(int easyMoveSuiteConfigTemplateId, long adminId, string fullname)
		{
			string sql = @"UPDATE easymovesuiteconfigtemplate SET Enabled=0,EditAdminId=?EditAdminId,EditAdminName=?EditAdminName WHERE EasyMoveSuiteConfigTemplateId=?EasyMoveSuiteConfigTemplateId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?EasyMoveSuiteConfigTemplateId", easyMoveSuiteConfigTemplateId, DbType.Int32);
			parameters.Add("?EditAdminId", adminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", fullname, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion
	}
}



